/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-11-27     xuan       the first version
 */
#include<rtthread.h>
#define SAMPLE_UART_NAME "uart1"

rt_sem_t sem1;
rt_device_t serial;

rt_err_t uart_input(rt_device_t dev,rt_size_t size){
    rt_err_t ret;
    ret= rt_sem_release(sem1);
    return ret;
}

void serial_thread_entry(void* parameter){
    char ch;
    while(1){
        while(rt_device_read(serial, -1, &ch, 1)!=1){
            rt_sem_take(sem1, RT_WAITING_FOREVER);
        }
        ch=ch+1;
        rt_device_write(serial, 0, &ch, 1);
    }
}

int uart_sample(int argc,char* argv[]){
    rt_err_t ret=RT_EOK;
    char uart_name[RT_NAME_MAX];
    char str[]="hello world!\r\n";

    if (argc==2) {
        rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
    } else {
        rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
    }

    serial=rt_device_find(uart_name);
    if (!serial) {
        rt_kprintf("find %s failed!\r\n",uart_name);
        return RT_ERROR;
    }
    sem1=rt_sem_create("sem1", 0, RT_IPC_FLAG_FIFO);
    rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
    rt_device_set_rx_indicate(serial, uart_input);
    rt_device_write(serial, 0, str, (sizeof(str)-1));

    rt_thread_t thread=rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);
    if(thread!=RT_NULL){
        rt_thread_startup(thread);
    }else {
        ret=RT_ERROR;
    }
    return ret;
}

MSH_CMD_EXPORT(uart_sample,uart device sample);
